﻿// HSS.Interlink.Web.FileDownload.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       FileDownload.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/22/2011
// ----------------------------------------------------------------------------
namespace HSS.Interlink.Web
{
	#region Using Directives
	using System;
	using System.Configuration;
	using System.Web;
	using System.Web.SessionState;
	#endregion

	#region FileDownload
	/// <summary>
	/// FileDownload processes download request syncronously.
	/// </summary>
	public sealed class FileDownload : InterlinkHandler
	{
		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		public FileDownload()
		{

		}
		#endregion

		#region InterlinkHandler
		/// <summary>
		/// This is called by the ASP.NET Runtime or from <see cref="FileDownloadAsync"/>.
		/// </summary>
		/// <param name="context">The current <see cref="HttpContext"/> for the request being processed</param>
		public override void ProcessRequest(HttpContext context)
		{
			FileQuery query;
			if (!this.TryParseQuery(context, out query))
				return;

			IDownloadHandler handler = this.GetHandler<IDownloadHandler>(query.HandlerKey);
			if (null == handler)
			{
				this.Fault(context, StatusCode_File_Not_Found);
				return;
			}

			switch (query.Command)
			{
				case FileQueryCommand.OpenFileInline:
					handler.OpenInline(context, query);
					break;

				case FileQueryCommand.OpenFileAsAttachment:
					handler.OpenAsAttachment(context, query);
					break;

				case FileQueryCommand.Prepare:
				case FileQueryCommand.GetSingleFile:
				case FileQueryCommand.GetMultipleFiles:
					handler.Download(context, query);
					break;

				default:
					this.Fault(context, StatusCode_File_Not_Found);
					break;
			}

		}
		#endregion
	}
	#endregion
}